/****************************************************************************
** @brief
****************************************************************************/

#ifndef IOTUPGRADEMAIN_H
#define IOTUPGRADEMAIN_H

#include <QThread>
#include <QFile>


class ChargeMain;

enum UpdaeType {
    kUpdateTypeInvalid = 0,
    kUpdateTypeTCU,     //正在进行TCU升级
    kUpdateTypeCCU,     //正在进行CCU升级
    kUpdateTypeDCU,
    kUpdateTypePMU,
    kUpdateTypeACDC,
    kUpdateTypeDCDC,
    kUpdateTypeOTA,
};




enum UpdateState {
    kUpdateWait = 0,    //等待升级
    kUpdatePre,         //升级前置处理
    kUpdateConnect,     //连接中
    kUpdatePollData,    //获取数据中
    kUpdatePollFailed,  //获取数据失败
    kUpdateSuccess,     //升级成功
    kUpdateFailed,      //升级失败
    kUpdateInstall,     //安装中
    KUpdatePost,        //升级后置处理
};

struct UpdateInfo {
    bool is_updateing; //是否正在升级
    bool is_security;  //是否进行安全升级
    int Certificate_download_count;
    enum UpdaeType type;
    enum UpdateState state;
    int progress;      //升级进度

    size_t size;       //升级包的大小
    size_t crc;        //升级包的校验值

    QString describe;  //升级描述
};

struct ECU_staus_t {
    enum UpdaeType name;
    char version[5 + 1];
    uint8_t address;
    uint8_t is_enabled;  //是否进行升级
};

struct updata_con_status_t {
    uint8_t address;
    enum    UpdaeType type;
    uint8_t receive_rvn          : 2;

    uint8_t boot_or_not          : 2;
    uint8_t is_enabled           : 2; //是否进行升级
    uint8_t successful_or_failed : 2;
    uint8_t con_num;
};

class IotUpgrade : public QThread
{
    Q_OBJECT

public:
    IotUpgrade(QList<ChargeMain *> *chargelist, QObject *parent = nullptr);
    ~IotUpgrade();

    ///
    /// \brief check_update_path 检查升级路径
    /// \return
    ///
    static bool check_local_update_path();
    static void check_local_update_test();
    ///
    /// \brief check_update_file 检查升级文件是否存储
    /// \return
    ///
    static bool check_local_update_file();

    bool close();
    ///
    /// \brief process_local_update 执行本地升级
    /// \return
    ///
    static bool process_local_update();

    static int downloadFileFromFTP(const char *server, const char *user, const char *password, const char *remoteFile);

    static QString replaceHostWithIP(const QString& urlString);

    static bool is_updateing()
    {
        return update_info_.is_updateing;
    }
    static void set_is_updateing(bool value)
    {
        update_info_.is_updateing = value;
    }

    static bool is_security()
    {
        return update_info_.is_security;
    }
    static void set_is_security(bool value)
    {
        update_info_.is_security = value;
        if (value == true) {
            update_info_.Certificate_download_count = 3;
        }
    }

    static int get_Certificate_count()
    {
        return update_info_.Certificate_download_count;
    }

    static  void set_Certificate_count( int value)
    {
        update_info_.Certificate_download_count = value;
    }

    static enum UpdaeType update_type()
    {
        return update_info_.type;
    }
    static void set_update_type(enum UpdaeType value)
    {
        update_info_.type = value;
    }

    static enum UpdateState update_state()
    {
        return update_info_.state;
    }
    static void set_update_state(enum UpdateState value)
    {
        update_info_.state = value;
    }

    static int update_progress()
    {
        return update_info_.progress;
    }
    static void set_update_progress(int value)
    {
        update_info_.progress = value;
    }

    static size_t update_size()
    {
        return update_info_.size;
    }
    static void set_update_size(size_t value)
    {
        update_info_.size = value;
    }

    static size_t update_crc()
    {
        return update_info_.crc;
    }
    static void set_update_crc(size_t value)
    {
        update_info_.crc = value;
    }

    static QString update_describe()
    {
        return update_info_.describe;
    }
    static void set_update_describe(const QString& value)
    {
        update_info_.describe = value;
    }

    static QFile update_file_;       //升级文件
    static QString update_name_;     //升级文件的文件名
    static QByteArray update_data_;  //升级文件的数据

protected:
    void run() Q_DECL_OVERRIDE;

private:
    bool m_quit;
    unsigned long m_cycle;

    QList<ChargeMain *> *charge_list_;

    QByteArray m_update_location_array;
    QByteArray m_update_req_array;
    QByteArray m_usuer_array;
    QByteArray m_pass_array;
    QString m_update_req_location;
    QString m_update_location;
    QString m_usuer;
    QString m_pass;

    //static int  Certificate_download_count_;

    static struct UpdateInfo update_info_;

signals:
    void StartUpdate(qint32 subversion, qint32 revision, qint32 packLength, qint32 checkSum, const QString& updateType);
    void JL_StartUpdate(bool ccu_enanble, bool dcu_enanble, bool pmu_enanble, bool acdc_enanble, bool dcdc_enanble);
    void SystemReboot();

public slots:

private slots:

};


#endif // IOTUPGRADEMAIN_H
